cmake_minimum_required(VERSION 2.8.7)
project(flecs_example)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)

include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)

macro(SUBDIRLIST result cur_dir)
    file(GLOB children LIST_DIRECTORIES true RELATIVE ${cur_dir} ${cur_dir}/*)
    set(dirlist "")
    foreach (child ${children})
        if (IS_DIRECTORY ${cur_dir}/${child})
            list(APPEND dirlist ${child})
        endif ()
    endforeach ()

    set(${result} ${dirlist})
endmacro()

SUBDIRLIST(c_subdirs ${CMAKE_CURRENT_SOURCE_DIR}/c)
SUBDIRLIST(cpp_subdirs ${CMAKE_CURRENT_SOURCE_DIR}/cpp)

get_filename_component(PARENT_DIR ../ ABSOLUTE)
macro(CREATE_TARGET name parent)
    set(target_name example_${parent}_${name})
    set(target_name_static example_${parent}_${name}_static)
    set(target_path ${CMAKE_CURRENT_SOURCE_DIR}/${parent}/${name})
    set(src_files "")
    aux_source_directory(${target_path}/src/ src_files)
    add_executable(${target_name} ${src_files})
    add_executable(${target_name_static} ${src_files})
    target_include_directories(${target_name} PUBLIC ${PARENT_DIR}/include)
    target_include_directories(${target_name_static} PUBLIC ${PARENT_DIR}/include)
    target_include_directories(${target_name} PUBLIC ${target_path}/include)
    target_include_directories(${target_name_static} PUBLIC ${target_path}/include)
    target_link_libraries(${target_name} flecs)
    target_link_libraries(${target_name_static} flecs_static)
endmacro()

foreach (c_subdir ${c_subdirs})
    CREATE_TARGET(${c_subdir} c)
endforeach ()

foreach (cpp_subdir ${cpp_subdirs})
    CREATE_TARGET(${cpp_subdir} cpp)
endforeach ()
